﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace Quanlybanhang
{
    public partial class Chuhangmua : Office2007Form
    {
        int id = 0;
        string ten = "";
        string diachi = "";
        string sodt = "";
        decimal sono = 0;

        public Chuhangmua()
        {
            InitializeComponent();
            txtTen.Enabled = false;
            txtDiachi.Enabled = false;
            txtDienthoai.Enabled = false;
            txtSono.Enabled = false;
            khoitao();
        }

        //Phuong thuc khoi tao cac gia tri ban dau
        public void khoitao()
        {
            //Dua vao cmbbox
            //Lay danh sach nhung chu hang ban hang - kieu = 2
            string cmb_cmd = "select distinct ten from chuhang where kieu = 1";
            SqlDataAdapter sda = new SqlDataAdapter(cmb_cmd, MainForm.con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            cmbChuhang.DataSource = dt;
            cmbChuhang.DisplayMember = "ten";
        }

        private void cmbChuhang_SelectedIndexChanged(object sender, EventArgs e)
        {
            String chuhang = cmbChuhang.Text;
            String cmd = "select id, ten, diachi, dienthoai, sono from chuhang where ten = '" + chuhang + "'";
            SqlCommand sqlcmd = new SqlCommand(cmd, MainForm.con);
            SqlDataReader rdr = sqlcmd.ExecuteReader();
            while (rdr.Read())
            {
                id = (int)(rdr["id"]);
                ten = (string)(rdr["ten"]);
                diachi = (string)(rdr["diachi"]);
                sodt = (string)(rdr["dienthoai"]);
                sono = (decimal)(rdr["sono"]);
            }
            
            txtTen.Text = ten;
            txtDiachi.Text = diachi;
            txtDienthoai.Text = sodt;
            txtSono.Text = sono.ToString();
            rdr.Close();
        }

        private void btnChinhsua_Click(object sender, EventArgs e)
        {
            txtTen.Enabled = true;
            txtDiachi.Enabled = true;
            txtDienthoai.Enabled = true;
            txtSono.Enabled = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult xoa = MessageBox.Show("Bạn có chắc chắn xóa chủ hàng?", "Xóa chủ hàng", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (xoa == DialogResult.Yes)
            {
                SqlCommand sqlComm = new SqlCommand();
                sqlComm = MainForm.con.CreateCommand();
                sqlComm.CommandText = @"DELETE CHUHANG WHERE id='" + id + "'";
                sqlComm.ExecuteNonQuery();
                khoitao();
            }
        }

        private void btnLuulai_Click(object sender, EventArgs e)
        {
            SqlCommand sqlComm = new SqlCommand();
            sqlComm = MainForm.con.CreateCommand();
            sqlComm.CommandText = @"UPDATE CHUHANG SET ten='" + txtTen.Text + "', diachi='"+txtDiachi.Text+"', dienthoai='"+txtDienthoai.Text+"', sono="+ Convert.ToDecimal(txtSono.Text) +" WHERE id='" + id + "'";
            sqlComm.ExecuteNonQuery();
            MessageBox.Show("Lưu lại thành công!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
